<template>
    <div id="racipes-list">
        <div class="rl-top">
            <div class="rl-search">
                <input type="text" @change="inputFlag" v-model="rlSearch" placeholder="请输入书谱名称或关键词">
                <span>|</span>
                <span class="el-icon-search" @click="searchFun()"></span>
            </div>
            <el-button class="refurbish" type="primary" @click="refurbish">刷新</el-button>
            <div class="ri-right">
                <button class="btn-act" @click="recipesAdd()">添加</button>
                <!-- <button>隐藏</button> -->
                <button :class="delBtnsClass" @click="delMenus" :disabled="delBtnsControl">删除</button>
            </div>
        </div>
        <div class="rl-content">
            <el-table ref="multipleTable"
                 v-loading="loading"
                element-loading-background="rgba(0, 0, 0, 0.4)"
                :data="tableData"
                tooltip-effect="dark"
                style="width: 100%"
                :header-row-class-name="tableRowHeaderClassName"
                :row-class-name="tableRowClassName"
                type=index
                @selection-change="handleSelectionChange">
                <el-table-column type="selection" width="55"></el-table-column>
                <el-table-column type="index" :index="indexMethod" label="序号" width="50"></el-table-column>
                <el-table-column prop="menuName" label="食谱名称" width="120"></el-table-column>
                <el-table-column prop="name" label="封面图" >
                  <template slot-scope="scope">
                    <img @click="viewImg(scope.row.menuSrc)" :src="scope.row.menuSrc?scope.row.menuSrc:'../../../assets/img/errorImg.png'" :onerror="errorImage" alt="">
                  </template>
                </el-table-column>
                <el-table-column prop="releaseTime" label="上传时间" width="160" ></el-table-column>
                <el-table-column label="食谱状态">
                  <template slot-scope="scope">
                    <span :class="scope.row.state==1? 'state-ok':(scope.row.state==0?'state-ing':'state-no')">
                      {{scope.row.state==1? '以上传':(scope.row.state==0?'待审核':'未通过')}}
                    </span>
                  </template>
                </el-table-column>
                <el-table-column prop="doh" label="浏览量" ></el-table-column>
                <!-- <el-table-column prop="thumbsUp" label="点赞量" ></el-table-column> -->
                <el-table-column prop="collection" label="收藏量" ></el-table-column>
                <el-table-column label="操作" width="200px">
                  <template slot-scope="scope">
                    <button class="control-btn change" @click="openChange(scope.row)">修改</button>
                    <button class="control-btn delete" @click="delMenu(scope.row.menuId, scope.row.userId)">删除</button>
                  </template>
                </el-table-column>
            </el-table>
            <div class="pageControl">
               <el-pagination
                background
                @current-change="handleCurrentChange"
                :current-page="current_page"
                :page-size="10"
                layout="prev, pager, next"
                :total="total">
              </el-pagination>
            </div>
        </div>
         <el-dialog title="食谱审核" :visible.sync="dialogFormVisible">
          <div class="changeWin">
            <div class="cW-row">
              <span>食谱图片：</span>
              <img :src="winShowData.menuSrc" alt="">
            </div>
            <div class="cW-row">
              <span>食谱名称：</span>
              <b> {{winShowData.menuName}} </b>
            </div>
            <div class="cW-row">
              <span>当前状态：</span>
              <b> {{winShowData.state==1? '以上传':(winShowData.state==0?'待审核':'未通过')}} </b>
            </div>
            <div class="cW-row">
              <span>操作：</span>
              <div class="pw-box">
                  <input type="radio" value="1" v-model="changeState" id="okInput" name="state">
                  <span></span>
              </div>
              <label for="okInput">允许发布</label>
              <div class="pw-box">
                  <input type="radio" value="-1" v-model="changeState" id="noInput" name="state">
                  <span></span>
              </div>
              <label for="noInput">拒绝发布</label>
            </div>
          </div>
          <div slot="footer" class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取 消</el-button>
            <el-button type="primary" @click="changeStateFun()">确 定</el-button>
          </div>
        </el-dialog>
        <el-dialog title="图片查看" :visible.sync="dialogFormVisibleImg">
          <img class="bigImg" :src="bigImg?bigImg:'../../../assets/img/errorImg.png'" :onerror="errorImage" alt="">
          <div slot="footer" class="dialog-footer">
            <el-button @click="dialogFormVisibleImg = false">取 消</el-button>
            <el-button type="primary" @click="closeImg()">确 定</el-button>
          </div>
        </el-dialog>
    </div>
</template>
<script>
import urlencode from 'urlencode'
export default {
  data () {
    return {
      tableData: [],
      rlSearch: '',
      multipleSelection: [],
      total: 0,
      current_page: 1,
      stateList: [['state-ok', '000'], ['state-ing', '111'], ['state-no', '222']],
      loading: true,
      // 0: 所有 1： 搜索,
      dataFlag: 0,
      errorImage: 'this.src="' + require('../../../assets/img/errorImg.png') + '"',
      dialogFormVisible: false,
      winShowData: '',
      changeState: 0,
      delBtnsControl: true,
      delBtnsClass: '',
      bigImg: '',
      dialogFormVisibleImg: false
    }
  },

  methods: {
    viewImg (key) {
      this.bigImg = key
      this.dialogFormVisibleImg = true
    },
    closeImg () {
      this.dialogFormVisibleImg = false
    },
    refurbish () {
      this.$router.go(0)
    },
    indexMethod (index) {
      return ((index + 1) + 10 * (this.current_page - 1))
    },
    toggleSelection (rows) {
      if (rows) {
        rows.forEach(row => {
          this.$refs.multipleTable.toggleRowSelection(row)
        })
      } else {
        this.$refs.multipleTable.clearSelection()
      }
    },
    handleSelectionChange (val) {
      this.multipleSelection = val
      if (val.length === 0) {
        this.delBtnsControl = true
        this.delBtnsClass = ''
      } else {
        this.delBtnsControl = false
        this.delBtnsClass = 'btn-act'
      }
      console.log(this.multipleSelection)
    },
    handleCurrentChange (val) {
      console.log(`当前页: ${val}`)
      this.current_page = val
      if (this.dataFlag === 0) {
        this.getData()
      } else {
        this.getDateSearch()
      }
    },
    getData () {
      this.$axios({
        method: 'get',
        url: '/Menu/Admin/findAllMenu',
        params: {
          pageNow: this.current_page,
          pageSize: 10
        }
      }).then(data => {
        console.log(data.data)
        if (data.data.code === 200) {
          console.log(data.data.object.list[0])
          this.loading = false
          this.tableData = data.data.object.list
          this.total = data.data.object.total
          this.current_page = data.data.object.prePage + 1
        }
      }).catch(error => {
        console.log(error)
      })
    },
    tableRowClassName () {
      return 'row-class'
    },
    tableRowHeaderClassName () {
      return 'header-row-class-name'
    },
    delMenu (menuId, userId) {
      this.$confirm('此操作将永久删除该食谱, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$axios({
          method: 'delete',
          url: '/Menu/User/deleteMenu',
          params: {
            menuId,
            userId
          }
        }).then(data => {
          if (data.data.code === 200) {
            this.$message({
              message: '删除成功',
              type: 'success'
            })
            this.getData()
          }
        }).catch(error => {
          console.log(error)
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    delMenus () {
      this.$confirm('此操作将永久选中的食谱, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        var arr = []
        console.log('--0')
        for (const key of this.multipleSelection) {
          arr.push(key.menuId)
        }
        arr = arr.toString()
        this.$axios({
          method: 'delete',
          url: '/Menu/User/batchDeleteMenu',
          params: {
            userId: 1,
            menuId: arr
          }
        }).then(data => {
          console.log(data)
          if (data.data.code === 200) {
            this.$message({
              message: '删除成功',
              type: 'success'
            })
            this.getData()
          }
        }).catch(error => {
          console.log(error)
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    searchFun () {
      this.loading = true
      this.getDateSearch()
    },
    getDateSearch () {
      this.$axios({
        method: 'get',
        url: '/Menu/Admin/adminFindMenuByNameLike',
        params: {
          pageNow: this.current_page,
          key: this.rlSearch,
          pageSize: 10
        }
      }).then(data => {
        if (data.data.code === 200) {
          console.log(data.data)
          this.loading = false
          this.tableData = data.data.object.list
          this.total = data.data.object.total
          this.current_page = data.data.object.prePage + 1
        }
      }).catch(error => {
        console.log(error)
      })
    },
    inputFlag () {
      this.pre_page = 1
      if (this.rlSearch === '') {
        this.dataFlag = 0
      } else {
        this.dataFlag = 1
      }
    },
    openChange (key) {
      console.log(key)
      this.winShowData = key
      this.dialogFormVisible = true
      this.changeState = key.state
    },
    changeStateFun () {
      if (this.changeState === this.winShowData.state) {
        // this.$message.error('窗口关闭，本次未修改')
        this.dialogFormVisible = false
      } else {
        this.$axios({
          method: 'post',
          url: '/Menu/Admin/verifyMenu',
          params: {
            menuId: this.winShowData.menuId,
            state: this.changeState
          }
        }).then(data => {
          console.log(data.data)
          if (data.data.code === 200) {
            this.$message({
              message: '修改成功',
              type: 'success'
            })
            this.getData()
            this.dialogFormVisible = false
          }
        }).catch(error => {
          console.log(error)
        })
      }
    },
    recipesAdd () {
      this.$router.push({
        name: 'RecipesAdd'
      })
    }
  },
  mounted () {
    this.getData()
    const urls = 'asd54562'
    console.log(urlencode(urls))
  }
}
</script>

<style lang="less" scoped>
.bigImg{
  width: 600px;
  display: block;
  margin: 0 auto;
  max-height: 500px;
}
#racipes-list{
    width: 98%;
    margin: 0 auto;
}
.rl-top{
    height: 50px;width: 100%;
    line-height:50px;
    margin-top: 20px;
    .rl-search{
        float: left;
        height: 30px;line-height:30px;
        border: 1px solid #BFBFBF;
        margin-top: 10px;
        border-radius:4px;
        overflow: hidden;
        input{
            color: #BFBFBF;
            padding-left: 5px;
        }
        span:nth-child(2){
            color: #BFBFBF;
        }
        span:nth-child(3){
            width: 30px;height:30px;
            line-height:30px;
            text-align:center;
            font-weight: bold;
            color: #FF9512;
            &:hover{
                cursor: pointer;
            }
        }
    }
    .ri-right{
        float: right;
        width: 200px;
        display:grid;
        height: 50px;line-height: 50px;
        grid-template-columns: repeat(2, 90px);
        place-content: space-around space-between;
        button{
            border-radius: 5px;
            height: 30px;line-height: 30px;
            text-align: center;
            background-color: #BFBFBF;
            color: #fff;
            &:hover{
                cursor: pointer;
            }
        }
        .btn-act{
            background-color: #FF9512;
        }
    }
}
.rl-content{
    .has-gutter tr{
        background-color: #E8E7E7 !important;
    }
    .control-btn{
        width: 80px;
        height: 30px;line-height: 30px;
        text-align: center;
        border-radius:5px;
        margin: 0 5px;
        color: #fff;
    }
    .change{
        background-color: #FF9512;
    }
    .delete{
        background-color: #C4C4C4;
    }
    .state-no .el-table_1_column_6>cell{color: #FFBD6c !important;}
    .cell{color: #FFBD6c !important}
    .state-ok{color: #2FC25B;}
    .state-ps{color: #FF4329;}
    .pageControl{
      li{
        &:hover{
          color: #FF4329 !important
        }
      }
    }
    .pageControl{
      text-align: right;
      margin: 20px 0;
    }
    .state-ing{color:#FFC176}
    .state-no{color:red}
}
.changeWin{
  width: 60%;
  margin: 0 auto;
  img{
    width: 50px;
    height: 50px;
    object-fit: cover
  }
  div{
    margin: 0 0 20px 0;
    span{
      display: inline-block;
      width: 100px;
      text-align: right;
    }
  }
  .pw-box{
    margin: 0;
    display: inline-block;
    width: 15px; height: 15px;
    // margin: 0 5px 0 0;
    position: relative;
    background-color: #fff;
    vertical-align:middle;
    border-radius: 100%;
    border: 1px solid #E5E3DF;
    input{
        opacity: 0;
        margin: 0;
        position: absolute;top: 0;left: 0;
        width: 100%;height: 100%;
        z-index: 100;
        background-color: red;
    }
    span{
        display:block;width: 8px;height:8px;
        border-radius:100%;
        background-color: #FFB31A;
        position: absolute;
        top: 50%;left: 50%;
        margin: -4px 0 0 -4px;
        z-index: 1;
    }
    input+span{
        opacity: 0;
    }
    input:checked+span{
        opacity: 1;
    }
  }
  label{
      padding:3px 10px 0 5px ;
      display: inline-block;
      font-weight: bold;
      color: rgba(0,0,0,0.4);
  }
}

</style>
